import { _decorator, Component, Node } from 'cc';
import { StateMachine } from 'db://assets/component/StateMachine/StateMachine';
import { StateMachineBuilder } from 'db://assets/component/StateMachine/StateMachineBuilder';
const { ccclass, property } = _decorator;

@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends Component {
    stateMachine: StateMachine;

    gameContext = {
        moveX: 0,
        moveY: 0,
        isGround: true,
        isAttack: false,
        isDash: false,
        isOnWall: false,
    };

    protected onLoad(): void {
        this.init();
    }
    start() {}

    init() {
        const enter = this.initEnter();

        this.stateMachine = new StateMachineBuilder('enter')
            .addState({ name: 'enter', subStateMachine: enter })
            .addState({ name: 'dash' })
            .addState({ name: 'attack' })
            .addTransition({
                from: 'enter',
                to: 'dash',
                condition: () => this.gameContext.isDash,
            })
            .addTransition({
                from: 'dash',
                to: 'enter',
                condition: () => !this.gameContext.isDash,
            })
            .addTransition({
                from: 'enter',
                to: 'attack',
                condition: () => this.gameContext.isAttack,
            })
            .addTransition({
                from: 'attack',
                to: 'enter',
                condition: () => !this.gameContext.isAttack,
            })

            .build();
    }

    // 常时状态 (移动,空中)
    initEnter() {
        const ground = this.initGround();
        const air = this.initAir();

        return new StateMachineBuilder('ground')
            .addState({ name: 'ground', subStateMachine: ground })
            .addState({ name: 'air', subStateMachine: air })
            .addTransition({
                from: 'air',
                to: 'ground',
                condition: () => this.gameContext.isGround,
            })
            .addTransition({
                from: 'ground',
                to: 'air',
                condition: () => !this.gameContext.isGround,
            })
            .build();
    }

    initGround() {
        return new StateMachineBuilder('idle')
            .addState({ name: 'idle' })
            .addState({ name: 'run' })
            .addTransition({
                from: '*',
                to: 'idle',
                condition: () => this.gameContext.moveX === 0,
            })
            .addTransition({
                from: '*',
                to: 'run',
                condition: () => this.gameContext.moveX !== 0,
            })
            .build();
    }

    initAir() {
        return new StateMachineBuilder('enter')
            .addState({ name: 'climb' })
            .addState({ name: 'jump_up' })
            .addState({ name: 'jump_down' })
            .addTransition({
                from: '*',
                to: 'jump_up',
                condition: () => {
                    return (
                        !this.gameContext.isOnWall &&
                        this.gameContext.moveY > 0
                    );
                },
            })
            .addTransition({
                from: '*',
                to: 'jump_down',
                condition: () => {
                    return (
                        !this.gameContext.isOnWall &&
                        this.gameContext.moveY < 0
                    );
                },
            })
            .addTransition({
                from: '*',
                to: 'climb',
                condition: () => {
                    return this.gameContext.isOnWall;
                },
            })
            .build();
    }

    getState() {
        if (this.stateMachine) {
            return this.stateMachine.getCurrentState();
        }
        return '';
    }

    update(deltaTime: number) {
        if (this.stateMachine) {
            this.stateMachine.update(deltaTime);
        }
    }
}
